package com.eascs.file;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.recordlog.constant.O2oLogType;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.dfs.fastdfs.FastDfsClient;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.datalist.dao.O2oDataListDao;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Repository
public class FileDao extends BaseO2oDao {
	private final Logger logger = LoggerFactory.getLogger(FileDao.class);
	
	@Autowired
	private O2oDataListDao o2oDataListDao;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	
	@Autowired
	private FastDfsClient fastDfsClient;

	private static String groupName = "grouploan";
	
	public String upload(MultipartFile file, String datalistCode, String loanapplyno) {
		User user = UserHolder.getUser();
		try {
			String extendName = fastDfsClient.getFileExtName(file.getOriginalFilename());
			String urlPath = fastDfsClient.uploadInputStream(file.getInputStream(), groupName, extendName, null);
			logger.info("FastDFS uploadPath:[{}]", urlPath);
			List<O2oDataListEntity> list = o2oDataListDao.findDateEntity(loanapplyno, datalistCode);
			O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyno);
			if (list != null && list.size() > 0) {
				if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_BZJDATA)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_CONTRACT)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_BC_CONTRACT)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_GUIDELINE)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_NSFJ)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_JQ_NSFJ)
						|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_RPMENT)
						|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_PICTURE)
						|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_EVIDENCE)
						|| datalistCode.equals(FileTypeConstants.BRAND_TYPE_RPMENT)
						|| datalistCode.equals(FileTypeConstants.LOAN_CHECK_TYPE_RPMENT)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_TQHK)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_DCWD)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_RPMENT)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_CREDITFILING)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_O2OPDR)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_FKZZWJ)
						|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_RPMENT)
						|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_PICTURE)
						|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_EVIDENCE)
						|| datalistCode.equals(FileTypeConstants.CLASSIFY_TYPE)
						|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_DK_PZ)
						/*|| datalistCode.equals(FileTypeConstants.ENTERPRISE_ZX_IDRC)
						|| datalistCode.equals(FileTypeConstants.ENTERPRISE_ZX_CRD)
						|| datalistCode.equals(FileTypeConstants.ENTERPRISE_ZX_ICRD)
						|| datalistCode.equals(FileTypeConstants.ENTERPRISE_ZX_IDCARD)*/
						) {
					if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)) {
						if (o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NEW)
								|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_BEGIN)) {
							datalistCode=FileTypeConstants.DALIST_TYPE_ZXFJ;
						}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_END)
								|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_BEGIN)){
							datalistCode=FileTypeConstants.DALIST_TYPE_IAFJ;
						}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_END)
								|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_SB_BEGIN)){
							datalistCode=FileTypeConstants.DALIST_TYPE_MQFJ;
						}else{
							datalistCode=FileTypeConstants.DALIST_TYPE_AFTER_LOAN_ADD;
						}
					}
					O2oDataListEntity loanEntity = new O2oDataListEntity();
					Date date = new Date();
					loanEntity.setCreatDate(date);
					loanEntity.setUpdateTime(date);
					loanEntity.setUrlpath(urlPath);
					loanEntity.setOriginalName(file.getOriginalFilename());
					loanEntity.setDatalistCode(datalistCode);
					loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
					loanEntity.setCreatId(user.getNo());
					loanEntity.setCreatOr(user.getCname());
					loanEntity.setExtendNmae(extendName);
					loanEntity.setLoanapplyNo(loanapplyno);
					loanEntity.setIsDelete(StatusContexts.O2O_NO);
					this.insert(loanEntity);
					insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + loanEntity.getDatalistNo());
				} else {
					for (int i = 0; i < list.size(); i++) {
						Date date = new Date();
						O2oDataListEntity loanEntity = list.get(i);
						loanEntity.setIsDelete(StatusContexts.O2O_YES);
						loanEntity.setUpdateTime(date);
						this.updateById(loanEntity);// 先隐藏=删除在新增一条数据
						insertLog(O2oLogType.FILE_STATUS_N, "更新，上传其他附件,ID:" + loanEntity.getDatalistNo());
						O2oDataListEntity dateList = new O2oDataListEntity();
						dateList.setUrlpath(urlPath);
						dateList.setOriginalName(file.getOriginalFilename());
						dateList.setDatalistCode(datalistCode);
						dateList.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
						dateList.setCreatId(user.getNo());
						dateList.setCreatOr(user.getCname());
						dateList.setCreatDate(date);
						dateList.setUpdateTime(date);
						dateList.setExtendNmae(extendName);
						dateList.setLoanapplyNo(loanapplyno);
						dateList.setIsDelete(StatusContexts.O2O_NO);
						this.insert(dateList);
						insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + dateList.getDatalistNo());
					}
				}
			} else {
				if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)) {
					if (o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NEW)
							|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_BEGIN)) {
						datalistCode=FileTypeConstants.DALIST_TYPE_ZXFJ;
					}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_ZX_END)
							|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_BEGIN)){
						datalistCode=FileTypeConstants.DALIST_TYPE_IAFJ;
					}else if(o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_NS_END)
							|| o2oLoanapplyEntity.getWfType().equals(StatusContexts.O2O_SB_BEGIN)){
						datalistCode=FileTypeConstants.DALIST_TYPE_MQFJ;
					}else{
						datalistCode=FileTypeConstants.DALIST_TYPE_AFTER_LOAN_ADD;
					}
				}
				O2oDataListEntity loanEntity = new O2oDataListEntity();
				Date date = new Date();
				loanEntity.setCreatDate(date);
				loanEntity.setUpdateTime(date);
				loanEntity.setUrlpath(urlPath);
				loanEntity.setOriginalName(file.getOriginalFilename());
				loanEntity.setDatalistCode(datalistCode);
				loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
				loanEntity.setCreatId(user.getNo());
				loanEntity.setCreatOr(user.getCname());
				loanEntity.setExtendNmae(extendName);
				loanEntity.setLoanapplyNo(loanapplyno);
				loanEntity.setIsDelete(StatusContexts.O2O_NO);
				this.insert(loanEntity);
				insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + loanEntity.getDatalistNo());
			}
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
		return "";
	}
	
	public String upload(MultipartFile file, String datalistCode, String loanapplyno, String paymentsId) {
		User user = UserHolder.getUser();
		try {
			String extendName = fastDfsClient.getFileExtName(file.getOriginalFilename());
			String urlPath = fastDfsClient.uploadInputStream(file.getInputStream(), groupName, extendName, null);
			logger.info("FastDFS uploadPath:[{}]", urlPath);
			List<O2oDataListEntity> list = o2oDataListDao.findDateEntity(loanapplyno, datalistCode, paymentsId);
			if (list != null && list.size() > 0) {
					for (int i = 0; i < list.size(); i++) {
						Date date=new Date();
						O2oDataListEntity loanEntity = list.get(i);
						loanEntity.setIsDelete(StatusContexts.O2O_YES);
						loanEntity.setUpdateTime(date);
						this.updateById(loanEntity);
						insertLog(O2oLogType.FILE_STATUS_N, "更新，上传其他附件,ID:" + loanEntity.getDatalistNo());
						O2oDataListEntity dateList = new O2oDataListEntity();
						dateList.setUrlpath(urlPath);
						dateList.setOriginalName(file.getOriginalFilename());
						dateList.setDatalistCode(datalistCode);
						dateList.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
						dateList.setCreatId(user.getNo());
						dateList.setCreatOr(user.getCname());
						dateList.setUpdateTime(date);
						dateList.setCreatDate(date);
						dateList.setExtendNmae(extendName);
						dateList.setLoanapplyNo(loanapplyno);
						dateList.setPaymentsId(paymentsId);
						dateList.setIsDelete(StatusContexts.O2O_NO);
						this.insert(dateList);
						insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + dateList.getDatalistNo());
					}
			} else {
				O2oDataListEntity loanEntity = new O2oDataListEntity();
				Date date=new Date();
				loanEntity.setCreatDate(date);
				loanEntity.setUpdateTime(date);
				loanEntity.setUrlpath(urlPath);
				loanEntity.setOriginalName(file.getOriginalFilename());
				loanEntity.setDatalistCode(datalistCode);
				loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
				loanEntity.setCreatId(user.getNo());
				loanEntity.setCreatOr(user.getCname());
				loanEntity.setExtendNmae(extendName);
				loanEntity.setLoanapplyNo(loanapplyno);
				loanEntity.setPaymentsId(paymentsId);
				loanEntity.setIsDelete(StatusContexts.O2O_NO);
				this.insert(loanEntity);
				insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + loanEntity.getDatalistNo());
			}
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
		return "";
	}
	
	public boolean updateFileEntity(O2oDataListEntity loanEntity){
		return this.updateById(loanEntity)>0?true:false;
	}
	
	public String upload_sponsor(MultipartFile file, String datalistCode, String loanapplyno,String sponsorId) {
		User user = UserHolder.getUser();
		try {
			String extendName = fastDfsClient.getFileExtName(file.getOriginalFilename());
			String urlPath = fastDfsClient.uploadInputStream(file.getInputStream(), groupName, extendName, null);
			logger.info("FastDFS uploadPath:[{}]", urlPath);
			O2oDataListEntity list = o2oDataListDao.findSponsorCode(loanapplyno, datalistCode,sponsorId);
			if (list != null ) {
					Date date=new Date();
					O2oDataListEntity loanEntity = list;
					loanEntity.setIsDelete(StatusContexts.O2O_YES);
					loanEntity.setUpdateTime(date);
					this.updateById(loanEntity);//先隐藏=删除在新增一条数据
					insertLog(O2oLogType.FILE_STATUS_N, "更新，上传其他附件,ID:" + loanEntity.getDatalistNo());
					O2oDataListEntity dateList = new O2oDataListEntity();
					dateList.setUrlpath(urlPath);
					dateList.setOriginalName(file.getOriginalFilename());
					dateList.setDatalistCode(datalistCode);
					dateList.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
					dateList.setCreatId(user.getNo());
					dateList.setCreatOr(user.getCname());
					dateList.setCreatDate(date);
					dateList.setUpdateTime(date);
					dateList.setExtendNmae(extendName);
					dateList.setLoanapplyNo(loanapplyno);
					dateList.setIsDelete(StatusContexts.O2O_NO);
					dateList.setSponsorId(sponsorId);
					this.insert(dateList);
					insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + dateList.getDatalistNo());
			} else {
				O2oDataListEntity loanEntity = new O2oDataListEntity();
				Date date=new Date();
				loanEntity.setCreatDate(date);
				loanEntity.setUpdateTime(date);
				loanEntity.setUrlpath(urlPath);
				loanEntity.setOriginalName(file.getOriginalFilename());
				loanEntity.setDatalistCode(datalistCode);
				loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
				loanEntity.setCreatId(user.getNo());
				loanEntity.setCreatOr(user.getCname());
				loanEntity.setExtendNmae(extendName);
				loanEntity.setLoanapplyNo(loanapplyno);
				loanEntity.setIsDelete(StatusContexts.O2O_NO);
				loanEntity.setSponsorId(sponsorId);
				this.insert(loanEntity);
				insertLog(O2oLogType.FILE_STATUS_N, "新建，上传其他附件,ID:" + loanEntity.getDatalistNo());
			}
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
		return "";
	}
	
	/**
	 * @Description: <p> 插入文件修改日志 </p>
	 * @author CXW
	 * @date 2016年9月26日 下午2:35:13
	 * @version V1.0
	 * @param logType 日志类型,updateStr 修改内容
	 * @return void
	 */
	public void insertLog(String logType,String updateStr){
		try {
			o2oLoanapplyService.saveLog(UserHolder.getUser().getCname(), logType, null, updateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
